<?php
	include("header.php");

	if(isset($_POST['name']))
	{
		$name = escape_data_filter($_POST['name']);

		$valid_errors = array();

		if(empty($name))
			$valid_errors[] = 'Полето (Доставчик) е празно!';

		if(count($valid_errors)==0)
		{
			if(empty($_GET['provider_id']))
			{
				// ДОБАВЯНЕ
				$query	= sprintf("INSERT INTO `providers` (`name`, `created`, `deleted`)
						VALUES('%s', %d, 0)", $name, strtotime("now"));

				mysql_query($query) or die(mysql_error());

				$message = '<div class="alert-box success">
						<span></span>Данните са успешно добавени.
					    </div>';

				$_SESSION['message'] = $message;
				$last_insert_id = mysql_insert_id();
				header('Location: providers_edit.php?provider_id='.$last_insert_id);
			}
			else{
				// ПРОМЯНА
				$query	= sprintf("UPDATE `providers` SET `name` = '%s' WHERE `provider_id` = %d", $name, $_GET['provider_id']);
				mysql_query($query) or die(mysql_error());

				$message = '<div class="alert-box warning">
						<span></span>Данните са успешно променени.
					    </div>';

				$_SESSION['message'] = $message;
				header('Location: providers_edit.php?provider_id='.$_GET['provider_id']);
			}
		}
	}
?>

	<div id="content">
		<div id="title">
			<?php
				// Доставчици
				$query		= sprintf("SELECT * FROM `providers` WHERE `provider_id` = %d", $_GET['provider_id']);
				$results	= mysql_query($query) or die(mysql_error());
				$num_rows	= mysql_num_rows($results);
				$fetch = array();
				if($num_rows){
					$fetch = mysql_fetch_assoc($results);

					echo "Доставчици >> Редактиране >> ".htmlspecialchars(stripslashes($fetch['name']));
				}
				else
				{
					$fetch['name'] = '';
					echo "Доставчици >> Добавяне";
				}
			?>
		</div>
		<form name="fsearch" method="POST">
			<fieldset class="fieldsetPanel" style="width: 410px;">
				<legend>
					<?php
						if(empty($_GET['provider_id']))
							echo "Добавяне";
						else
							echo "Промяна";
					?>
				</legend>
				<table style="font-size: 12px;" border="0">
					<?php
						if (!empty($valid_errors))
						{
					?>
					<tr>
						<td colspan="4">

							<div class="error_message">
								<p>Валидиране на входните данни:</p>
								<ul>
									<?php
							 			foreach ($valid_errors as $error) {
											echo "<li>$error</li>";
							 			}
									?>
								</ul>
							</div>
						</td>
					</tr>
					<?php
						}
						else{
							if(!empty($_SESSION['message'])){
					?>
					<tr>
						<td align="center" style="color: #78cb01;" colspan="4">
							<b><?php echo $_SESSION['message']; ?></b><br/>
						</td>
					</tr>
					<?php
							}
						}
					?>
					<tr>
						<td width="80" height="25">Доставчик:&nbsp;<span style="font-weight:bold; color: #ff0000;">*</span></td>
						<td width="320"><input type="text" name="name" class="input" value="<?php echo (isset($_POST['name']) ? htmlspecialchars(stripslashes($name)) : htmlspecialchars(stripslashes($fetch['name']))); ?>" style="width: 320px;" maxlength="255" /></td>
					</tr>
					<tr>
						<?php
							if(!empty($_GET['provider_id'])){
						?>
								<td height="25">Дата:</td>
								<td><b><?php echo date('d-m-Y H:i', $fetch['created']); ?></b></td>
						<?php
							}
						?>
					</tr>
					<tr>
						<td height="40" align="right" colspan="4"><br/>
							<input type="button" name="btn_cancel" class="btn" value="Отказ" onClick="window.location = 'providers.php'" />&nbsp;&nbsp;
							<input type="submit" name="btn_submit" class="btn" value="<?php echo (empty($_GET['provider_id']) ? 'Добавяне' : 'Промени'); ?>" />
						</td>
					</tr>
				</table>
			</fieldset>
		</form>
		<div id="getContent"></div>

		<?php require_once("footer.php"); ?>